<template>
  <div>
    <el-dialog
    v-model="visibleDialog"
    :title="`${form.id?'修改':'添加'}收货地址`"
    width="30%"
  >
      <!-- <span>This is a message</span> -->
      <!-- 对话框内容 切换收货地址-->
      <el-form :model="form">
      <el-form-item label="收货人：" :label-width="formLabelWidth">
        <el-input v-model="form.receiver" placeholder="请输入收货人" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号：" :label-width="formLabelWidth">
        <el-input v-model="form.contact" placeholder="请输入手机号" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="地区：" :label-width="formLabelWidth">
        <City placeholder="请选择所在地区" :fullLocation="form.fullLocation" @change="changeCty"></City>
      </el-form-item>
      <el-form-item label="详细地址：" :label-width="formLabelWidth">
        <el-input v-model="form.address" placeholder="请输入详细地址" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="邮政编码：" :label-width="formLabelWidth">
        <el-input v-model="form.postalCode" placeholder="请输入邮政编码" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="地址标签：" :label-width="formLabelWidth">
        <el-input v-model="form.addressTags" placeholder="请输入地址标签，逗号分隔" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visibleDialog = false">取消</el-button>
          <el-button type="primary" @click="submit"
            >确认</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive } from 'vue-demi'
import { ElMessage } from 'element-plus'
import { addAddress, editAddress } from '../../../../api/order'
export default {
  name: 'AddressEdit',
  setup (props, { emit }) {
    const visibleDialog = ref(false)
    // emit('')
    const open = (showAdd) => {
      visibleDialog.value = true
      // 如果是添加  在打开之前清空
      // console.log(1)
      // console.log(showAdd)
      if (showAdd.id) {
        // 修改数据 填充表单
        for (const key in showAdd) {
          if (Object.hasOwnProperty.call(showAdd, key)) {
            form[key] = showAdd[key]
          }
        }
      } else {
        for (const key in form) {
          if (Object.hasOwnProperty.call(form, key)) {
            if (key === 'isDefault') {
              form[key] = 0
            } else {
              form[key] = ''
            }
          }
        }
      }
    }
    // 表单内容
    const form = reactive({
      // id: '',
      receiver: '',
      contact: '',
      provinceCode: '',
      cityCode: '',
      countyCode: '',
      address: '',
      postalCode: '',
      addressTags: '',
      isDefault: 0,
      fullLocation: ''
    })
    const formLabelWidth = ref('100px')
    // 更改城市时更新
    const changeCty = (data) => {
      // console.log(data)
      form.provinceCode = data.provinceCode
      form.cityCode = data.cityCode
      form.countyCode = data.countyCode
      form.fullLocation = data.fullLocation
      // console.log(form.fullLocation)
    }
    // 提交修改
    const submit = () => {
      // 判断是否有id 没有说明是新添加  修改说明是有id
      // 修改
      if (form.id) {
        editAddress(form).then(() => {
          // 为当前数据加上id
          // form.id = data.result.id
          ElMessage({
            message: '修改配送地址成功',
            type: 'success'
          })
          visibleDialog.value = false
          // 触发自定义事件
          emit('on-success', form)
        })
      } else {
        // 添加
        addAddress(form).then((data) => {
          // 为当前数据加上id
          form.id = data.result.id
          ElMessage({
            message: '添加配送地址成功',
            type: 'success'
          })
          visibleDialog.value = false
          // 触发自定义事件
          emit('on-success', form)
        })
      }
    }
    return { visibleDialog, open, form, formLabelWidth, changeCty, submit }
  }
}
</script>

<style lang="less" scoped>

</style>
